<?php
class Tourism_freight_template_model extends BZ_Model
{
    protected $table = 'tourism_freight_template';
    protected $table_2 = 'tourism_freight_price_list';
    protected $table_3 = 'user';

    /*
     * 运费模版数量
     * */
    public function total($param)
    {
        $this->db->from($this->table . ' AS tourism_freight_template');
        $this->db->join($this->table_3 . ' AS user', 'tourism_freight_template.uid = user.uid');
        if (!empty($param['name'])) {
            $this->db->like('tourism_freight_template.name', trim($param['name']));
        }
        if (!empty($param['uid'])) {
            $this->db->where('user.uid', (int)$param['uid']);
        }
        return $this->db->count_all_results();
    }

    /*
     * 运费模版分页
     * */
    public function page_list($page_num, $num, $param = array())
    {
        $this->db->select('user.user_name, user.alias_name, tourism_freight_template.*');
        $this->db->from($this->table . ' as tourism_freight_template');
        if (isset($param['name'])) {
            $this->db->like('tourism_freight_template.name', trim($param['name']));
        }
        if (isset($param['uid'])) {
            $this->db->where('user.uid', (int)$param['uid']);
        }
        $this->db->join($this->table_3 . ' as user', 'tourism_freight_template.uid = user.uid');
        $this->db->order_by('tourism_freight_template.id', 'DESC');
        $this->db->limit($page_num, $num);
        return $this->db->get();
    }

    /*
     * 运费模版增加
     * */
    public function addTemplate($template_param)
    {
        $this->db->insert($this->table, $template_param);
        return $insert_id = $this->db->insert_id();
    }

    /*
     *运费模版修改
     * */
    public function updateTemplate($id, $template_param)
    {
        $this->db->where('id', $id);
        return $this->db->update($this->table, $template_param);
    }

    public function getTemplate($id = 0)
    {
        $this->db->select('u.alias_name,t.*');
        $this->db->from($this->table . ' as t');
        $this->db->join($this->table_3 . ' as u', 't.uid = u.uid');
        $this->db->where('id', $id);
        return $this->db->get();
    }
    /*
     *根据用户uid获取模版
     * */
    public function getTransport($uid = 0)
    {
        $this->db->where('uid', $uid);
        return $this->db->get($this->table);
    }
    /*
     *根据用户uid获取模版 模版id
     * */
    public function getTransports($params)
    {

        $this->db->where('uid', $params['uid']);
        $this->db->where('id', $params['id']);
        return $this->db->get($this->table)->row();
    }

}